package com.loongnoy.config;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        Object user = request.getSession().getAttribute("username");
        if (user == null) {   //未登陆，返回登陆页面
            request.getRequestDispatcher("/").forward(request, response);
            return false;
        } else {  //已登陆，放行请求
            return true;
        }
    }
}